import GlobalData from "../../GlobalData";
import NetAdapter from "../../adpapter/NetAdapter";
import { AlertManager, AlertType } from "../../alert/AlertManager";
import { MVC } from "../../framework/MVC";
import AdManager from "../../manager/AdManadgr";
import { GameVoManager } from "../../manager/GameVoManager";
import { Manager } from "../../manager/Manager";
import RuzhuItem from "./RuzhuItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class RuzhuView extends MVC.BaseView {

    @property(cc.Node)
    item: cc.Node = null;

    @property(cc.Node)
    layout: cc.Node = null;

    @property(cc.Label)
    tipLabel: cc.Label = null;

    @property(cc.Label)
    coinLabel: cc.Label = null;

    public pageItemNum: number = 10;
    private items: cc.Node[] = [];
    private ID: number = 0;
    private coin: number = 0;
    private myId: number = 0;

    protected changeListener(enable: boolean): void {
    }

    /*
     * 打开界面回调，每次打开只调用一
     */
    public onOpen(args: any): void {
        this.ID = args;
        super.onOpen(args);
        this.setInfo(args);
        console.log("RuzhuView args", args);
        this.initList();
        this.moveIntoUserList();

        let d = [, 50, 60, 70, 80, 90, 100];
        this.tipLabel.string = `玩家入驻后每小时获得自己战力值的${d[Number(this.ID)]}%金币`
    }

    public initList() {
        this.layout.removeAllChildren();
        if (this.items.length <= 0) {
            for (let i = 0; i < this.pageItemNum; i++) {
                let item = cc.instantiate(this.item);
                this.items.push(item);
                item.getComponent(RuzhuItem).registerTouch();
                item.parent = this.layout;
                item.active = false;
            }
        }
    }

    moveIntoUserList(updateUI = true, callback?: Function) {
        this.layout.children.forEach((item, index) => {
            item.active = false;
        })
        NetAdapter.moveIntoUserList({ island_id: this.ID }).then((res: any) => {
            console.log(this.ID + "岛屿信息", res);

            let inList = [], outList = [], findMe = false, coin = 0, myId = 0;
            GlobalData.inst.findMe = false;
            for (let i = 0; i < res.length; i++) {
                const item = res[i];
                const isMe = item.uid == GlobalData.inst.uid;
                if (isMe) {
                    coin = Number(item.receive_num);
                    myId = item.id;
                }
                if (item.user_status == "1") {//入驻岛屿
                    inList.push(item);
                    if (isMe) {
                        findMe = true;
                        GlobalData.inst.findMe = true;
                    }
                } else {
                    outList.push(item);
                }
            }
            console.log("我在不在岛屿？", findMe);
            this.coin = coin;
            this.myId = myId;
            this.coinLabel.string = "领取" + coin + "金币";

            if (updateUI) {
                for (let i = 0; i < inList.length; i++) {
                    const itemData = inList[i];
                    itemData["island_id"] = this.ID;
                    let item = this.layout.children[i]
                    item.getComponent(RuzhuItem).setData(itemData);
                }
                if (inList.length < this.pageItemNum && !findMe) {
                    this.addMoveIntoUser();
                }

            callback && callback();
        }).catch((err) => {
        })
    }

    addMoveIntoUser(to_uid = null) {
        let senddata = { island_id: this.ID }
        senddata["uid"] = GlobalData.inst.uid;
        if (to_uid) {
            senddata["to_uid"] = to_uid;
        }
        console.log("入驻请求", senddata);
        NetAdapter.addMoveIntoUser(senddata).then((res: any) => {
            console.log(this.ID + "入驻信息", res);
            this.moveIntoUserList();
            let msg = res.msg;
            // if (res.code == 0) {
                msg = msg || "入驻成功";
            // }
            AlertManager.showNormalTips(msg);
        }).catch((err) => {
        })
    }

    onClickGetReward() {
        if (this.coin < 1) {
            AlertManager.showNormalTips("请等待金币产生");
            // this.moveIntoUserList();
            return;
        }
        if (GlobalData.inst.tempHide) {
            AlertManager.showAlert(AlertType.SELECT, {
                reasonDesc: `领取奖励`,
                wayDesc: "是否领取" + this.coin + "金币?",
                confirmText: "领取",
                cancelText: "取消",
                showVideo: false,
                showClose: false,
                confirm: () => {
                    AdManager.inst.playReward(() => {
                        this.collectCoins(false);
                    })
                },
                cancel: () => {
                }
            });
        }
        AlertManager.showAlert(AlertType.SELECT, {
            reasonDesc: `领取奖励`,
            wayDesc: "是否领取" + this.coin + "金币?",
            confirmText: "1.5倍领取",
            cancelText: "领取",
            showVideo: true,
            showClose: true,
            confirm: () => {
                AdManager.inst.playReward(() => {
                    this.collectCoins(true);
                })
            },
            cancel: () => {
                this.collectCoins(false);
            }
        });
    }

    collectCoins(isAd = false) {
        let coin = this.coin;
        if (isAd) {
            coin *= 1.5;
        }
        NetAdapter.collectCoins({
            id: this.myId
        }).then((res) => {
            console.log("collectCoins", res);
            console.log("add coin", coin);
            GameVoManager.getInstance.setGold(coin);
            coin = 0;
            this.coin = coin;
            this.coinLabel.string = "领取" + coin + "金币";
        });
        // this.moveIntoUserList(false, () => {
        //     let coin = this.coin;
        //     if (isAd) {
        //         coin *= 1.5;
        //     }
        //     NetAdapter.collectCoins({
        //         id: this.myId
        //     }).then((res) => {
        //         console.log("collectCoins", res);
        //         console.log("add coin", coin);
        //         GameVoManager.getInstance.setGold(coin);
        //         this.moveIntoUserList();
        //     });
        // });
    }

    /*
     * 关闭界面回调，每次打开只调用一次
     */
    public onClose(): void {
        Manager.audio.playAudio(501);
        super.onClose();
    }

    public onBack() {
        this.onClose();
    }

}